﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using AutoR.Helpers;
using AutoR.Messages;
using AutoR.Models;
using AutoR.Services;
using LiteDB;
using Prism.Mvvm;
using Xamarin.Forms;

namespace AutoR.Tasks
{
	// Token: 0x020000FC RID: 252
	public class DrillTask : BindableBase
	{
		// Token: 0x1700016C RID: 364
		// (get) Token: 0x06000546 RID: 1350 RVA: 0x000408F6 File Offset: 0x0003EAF6
		protected GameData Game
		{
			get
			{
				return Runtime.Game;
			}
		}

		// Token: 0x1700016D RID: 365
		// (get) Token: 0x06000547 RID: 1351 RVA: 0x00054FB2 File Offset: 0x000531B2
		// (set) Token: 0x06000548 RID: 1352 RVA: 0x00054FBC File Offset: 0x000531BC
		public int Id
		{
			[CompilerGenerated]
			get
			{
				return this.<Id>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Id>k__BackingField == value)
				{
					return;
				}
				this.<Id>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Id);
			}
		}

		// Token: 0x1700016E RID: 366
		// (get) Token: 0x06000549 RID: 1353 RVA: 0x00054FE9 File Offset: 0x000531E9
		// (set) Token: 0x0600054A RID: 1354 RVA: 0x00054FF4 File Offset: 0x000531F4
		public string Type
		{
			[CompilerGenerated]
			get
			{
				return this.<Type>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (string.Equals(this.<Type>k__BackingField, value, StringComparison.Ordinal))
				{
					return;
				}
				this.<Type>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Type);
			}
		}

		// Token: 0x1700016F RID: 367
		// (get) Token: 0x0600054B RID: 1355 RVA: 0x00055025 File Offset: 0x00053225
		// (set) Token: 0x0600054C RID: 1356 RVA: 0x00055030 File Offset: 0x00053230
		public bool Enabled
		{
			[CompilerGenerated]
			get
			{
				return this.<Enabled>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Enabled>k__BackingField == value)
				{
					return;
				}
				this.<Enabled>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Enabled);
			}
		}

		// Token: 0x17000170 RID: 368
		// (get) Token: 0x0600054D RID: 1357 RVA: 0x0005505D File Offset: 0x0005325D
		// (set) Token: 0x0600054E RID: 1358 RVA: 0x00055068 File Offset: 0x00053268
		[BsonIgnore]
		public DateTime StarTime
		{
			[CompilerGenerated]
			get
			{
				return this.<StarTime>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (DateTime.Equals(this.<StarTime>k__BackingField, value))
				{
					return;
				}
				this.<StarTime>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.StarTime);
			}
		}

		// Token: 0x17000171 RID: 369
		// (get) Token: 0x0600054F RID: 1359 RVA: 0x00055098 File Offset: 0x00053298
		// (set) Token: 0x06000550 RID: 1360 RVA: 0x000550A0 File Offset: 0x000532A0
		[BsonIgnore]
		public int Number
		{
			[CompilerGenerated]
			get
			{
				return this.<Number>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Number>k__BackingField == value)
				{
					return;
				}
				this.<Number>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Number);
			}
		}

		// Token: 0x17000172 RID: 370
		// (get) Token: 0x06000551 RID: 1361 RVA: 0x000550CD File Offset: 0x000532CD
		// (set) Token: 0x06000552 RID: 1362 RVA: 0x000550D8 File Offset: 0x000532D8
		public int FleetID
		{
			[CompilerGenerated]
			get
			{
				return this.<FleetID>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<FleetID>k__BackingField == value)
				{
					return;
				}
				this.<FleetID>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.FleetID);
			}
		}

		// Token: 0x17000173 RID: 371
		// (get) Token: 0x06000553 RID: 1363 RVA: 0x00055105 File Offset: 0x00053305
		// (set) Token: 0x06000554 RID: 1364 RVA: 0x00055110 File Offset: 0x00053310
		public int Formation
		{
			[CompilerGenerated]
			get
			{
				return this.<Formation>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<Formation>k__BackingField == value)
				{
					return;
				}
				this.<Formation>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.Formation);
			}
		}

		// Token: 0x17000174 RID: 372
		// (get) Token: 0x06000555 RID: 1365 RVA: 0x0005513D File Offset: 0x0005333D
		// (set) Token: 0x06000556 RID: 1366 RVA: 0x00055148 File Offset: 0x00053348
		public bool IsNightWar
		{
			[CompilerGenerated]
			get
			{
				return this.<IsNightWar>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<IsNightWar>k__BackingField == value)
				{
					return;
				}
				this.<IsNightWar>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.IsNightWar);
			}
		}

		// Token: 0x17000175 RID: 373
		// (get) Token: 0x06000557 RID: 1367 RVA: 0x00055175 File Offset: 0x00053375
		// (set) Token: 0x06000558 RID: 1368 RVA: 0x00055180 File Offset: 0x00053380
		public int RepairStrategy
		{
			[CompilerGenerated]
			get
			{
				return this.<RepairStrategy>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<RepairStrategy>k__BackingField == value)
				{
					return;
				}
				this.<RepairStrategy>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.RepairStrategy);
			}
		}

		// Token: 0x17000176 RID: 374
		// (get) Token: 0x06000559 RID: 1369 RVA: 0x000551AD File Offset: 0x000533AD
		// (set) Token: 0x0600055A RID: 1370 RVA: 0x000551B8 File Offset: 0x000533B8
		public ObservableCollection<RuleFleet> RuleFleet
		{
			[CompilerGenerated]
			get
			{
				return this.<RuleFleet>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<RuleFleet>k__BackingField, value))
				{
					return;
				}
				this.<RuleFleet>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.RuleFleet);
			}
		}

		// Token: 0x17000177 RID: 375
		// (get) Token: 0x0600055B RID: 1371 RVA: 0x000551E8 File Offset: 0x000533E8
		// (set) Token: 0x0600055C RID: 1372 RVA: 0x000551F0 File Offset: 0x000533F0
		public DrillRule[] DrillRule
		{
			[CompilerGenerated]
			get
			{
				return this.<DrillRule>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (object.Equals(this.<DrillRule>k__BackingField, value))
				{
					return;
				}
				this.<DrillRule>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.DrillRule);
			}
		}

		// Token: 0x17000178 RID: 376
		// (get) Token: 0x0600055D RID: 1373 RVA: 0x00055220 File Offset: 0x00053420
		// (set) Token: 0x0600055E RID: 1374 RVA: 0x00055228 File Offset: 0x00053428
		public bool FlagshipSL
		{
			[CompilerGenerated]
			get
			{
				return this.<FlagshipSL>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<FlagshipSL>k__BackingField == value)
				{
					return;
				}
				this.<FlagshipSL>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.FlagshipSL);
			}
		}

		// Token: 0x17000179 RID: 377
		// (get) Token: 0x0600055F RID: 1375 RVA: 0x00055255 File Offset: 0x00053455
		// (set) Token: 0x06000560 RID: 1376 RVA: 0x00055260 File Offset: 0x00053460
		public int SLNumber
		{
			[CompilerGenerated]
			get
			{
				return this.<SLNumber>k__BackingField;
			}
			[CompilerGenerated]
			set
			{
				if (this.<SLNumber>k__BackingField == value)
				{
					return;
				}
				this.<SLNumber>k__BackingField = value;
				this.OnPropertyChanged(<>PropertyChangedEventArgs.SLNumber);
			}
		}

		// Token: 0x06000561 RID: 1377 RVA: 0x00055290 File Offset: 0x00053490
		public async Task Start()
		{
			List<PVPOpponent> source = await Runtime.Remote.GetPVPList();
			List<PVPOpponent> json = (from i in source
			where i.resultLevel == WarResultLevel.none
			select i).ToList<PVPOpponent>();
			if (json.Any<PVPOpponent>())
			{
				MessagingCenter.Send<LogMessage>(new LogMessage("[演习]开始演习"), "Log");
				await this.Game.SupplyFleet(this.FleetID, 0);
				await this.Game.Delay(500);
				using (List<PVPOpponent>.Enumerator enumerator = json.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						DrillTask.<>c__DisplayClass54_0 CS$<>8__locals1 = new DrillTask.<>c__DisplayClass54_0();
						CS$<>8__locals1.ds = enumerator.Current;
						bool flag = true;
						int mofl = this.FleetID;
						int mofor = this.Formation;
						bool moin = this.IsNightWar;
						MessagingCenter.Send<LogMessage>(new LogMessage("[详细]敌方阵容：" + string.Join(", ", from i in CS$<>8__locals1.ds.ships
						select i.Type.ToName())), "FullLog");
						for (int j = 0; j < this.DrillRule.Length; j++)
						{
							DrillRule drillRule = this.DrillRule[j];
							if (drillRule.NodeCondition.Count > 0)
							{
								int[] id = new int[]
								{
									1,
									2,
									3,
									4,
									5,
									6,
									7,
									8,
									9,
									10,
									11,
									12,
									13,
									14,
									15,
									16,
									23,
									24
								};
								if (drillRule.NodeCondition.Select(delegate(NodeCondition i)
								{
									int num = CS$<>8__locals1.ds.ships.Count((UserShip y) => y.Type == (ShipType)id[i.ShipType]);
									if (i.Operator == 0)
									{
										return num > i.Number;
									}
									if (i.Operator != 1)
									{
										return num < i.Number;
									}
									return num == i.Number;
								}).All((bool i) => i))
								{
									MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[演习]地方阵容满足配置{0},切换中", j + 1)), "Log");
									await Extensions.GenerateFleet(drillRule.RuleFleet, drillRule.FleetID, false);
									mofl = drillRule.FleetID;
									mofor = drillRule.Formation;
									moin = drillRule.IsNightWar;
									flag = false;
									break;
								}
							}
							drillRule = null;
						}
						if (flag)
						{
							MessagingCenter.Send<LogMessage>(new LogMessage("[演习]回到默认配置,切换中"), "Log");
							await Extensions.GenerateFleet(this.RuleFleet, this.FleetID, false);
						}
						if (this.RepairStrategy == 2)
						{
							List<UserShip> list = (from i in this.Game.Fleet[this.FleetID].UserShip
							where !i.IsInRepair && i.IsShipNeedRepair
							select i).ToList<UserShip>();
							if (list.Any<UserShip>())
							{
								foreach (UserShip item in list)
								{
									if (!this.Game.RepairTask.RepairQueue.Contains(item))
									{
										this.Game.RepairTask.RepairQueue.Enqueue(item);
									}
								}
								MessagingCenter.Send<LogMessage>(new LogMessage("[详细]演习队伍有船需要进入船坞修理"), "FullLog");
								return;
							}
						}
						else
						{
							List<UserShip> source2 = (from i in this.Game.Fleet[this.FleetID].UserShip
							where i.IsInRepair || i.IsShipNeedRepair
							select i).ToList<UserShip>();
							if (source2.Any<UserShip>())
							{
								if (this.RepairStrategy == 1)
								{
									source2 = (from i in source2
									where i.BrokenType == ShipBrokenType.BigBroken
									select i).ToList<UserShip>();
								}
								await this.Game.InstantRepairShip((from i in source2
								select i.id).ToList<int>());
							}
						}
						if (this.Game.Fleet[this.FleetID].UserShip.Any((UserShip i) => i.IsInRepair || i.BrokenType == ShipBrokenType.BigBroken))
						{
							MessagingCenter.Send<LogMessage>(new LogMessage("[详细]演习队伍有船正在修理或大破"), "FullLog");
							return;
						}
						int slnum = 1;
						bool sl;
						do
						{
							sl = false;
							MessagingCenter.Send<LogMessage>(new LogMessage("[演习]开始挑战" + CS$<>8__locals1.ds.username), "Log");
							MessagingCenter.Send<LogMessage>(new LogMessage("[详细]开始索敌"), "FullLog");
							FoundEnemyVO spy = await Runtime.Remote.PVPSpy(mofl + 1, (long)CS$<>8__locals1.ds.uid);
							await this.Game.Delay(2000);
							List<ShipType> list2 = (from i in spy.enemyShips
							select i.type).ToList<ShipType>();
							MessagingCenter.Send<LogMessage>(new LogMessage("[详细]开打..."), "FullLog");
							WarReport cha;
							if (this.Game.GlobalConfig.AutoOneColume && list2.Contains(ShipType.Submarine))
							{
								cha = await Runtime.Remote.PVPChallenge(CS$<>8__locals1.ds.uid, mofl + 1, AutoR.Models.Formation.OneColume);
							}
							else
							{
								cha = await Runtime.Remote.PVPChallenge(CS$<>8__locals1.ds.uid, mofl + 1, mofor + AutoR.Models.Formation.OneRow);
							}
							if (this.FlagshipSL && cha.hpBeforeNightWarEnemy[0] > 0)
							{
								if (slnum > this.SLNumber)
								{
									MessagingCenter.Send<LogMessage>(new LogMessage("[演习]超过SL次数，放弃"), "Log");
								}
								else
								{
									MessagingCenter.Send<LogMessage>(new LogMessage("[演习]旗舰未消灭，重置..."), "Log");
									await this.Game.Delay(2000);
									await this.Game.GoHome();
									await this.Game.Delay(5000);
									slnum++;
									sl = true;
								}
							}
							else
							{
								await this.Game.Delay(new Random().Next(this.Game.GlobalConfig.MinSeconds * 1000, this.Game.GlobalConfig.MaxSeconds * 1000));
								MessagingCenter.Send<LogMessage>(new LogMessage("[详细]获取战斗结果"), "FullLog");
								BattleResult battleResult = await Runtime.Remote.PVPWarResult(moin && cha.canDoNightWar);
								this.Game.UserShip.UpdateUserShip(battleResult.shipVO);
								MessagingCenter.Send<LogMessage>(new LogMessage(string.Format("[演习]完成{0}挑战，结果:【{1}】", CS$<>8__locals1.ds.username, battleResult.warResult.resultLevel)), "Log");
								spy = null;
								cha = null;
							}
						}
						while (sl);
						await this.Game.Delay(3000);
						CS$<>8__locals1 = null;
					}
				}
				List<PVPOpponent>.Enumerator enumerator = default(List<PVPOpponent>.Enumerator);
			}
			if (DateTime.Now.Hour < 12)
			{
				this.StarTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 12, 2, 0);
			}
			else if (DateTime.Now.Hour < 18)
			{
				this.StarTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 18, 2, 0);
			}
			else
			{
				DateTime dateTime = DateTime.Now.AddDays(1.0);
				this.StarTime = new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, 0, 2, 0);
			}
		}

		// Token: 0x06000562 RID: 1378 RVA: 0x000552D8 File Offset: 0x000534D8
		public DrillTask()
		{
			this.<Id>k__BackingField = 3;
			this.<Type>k__BackingField = "DrillTask";
			this.<StarTime>k__BackingField = DateTime.Now;
			this.<RuleFleet>k__BackingField = new ObservableCollection<RuleFleet>();
			this.<DrillRule>k__BackingField = new DrillRule[]
			{
				new DrillRule(),
				new DrillRule(),
				new DrillRule()
			};
			this.<SLNumber>k__BackingField = 5;
			base..ctor();
		}
	}
}
